#!/usr/bin/env python2
# -*- coding: utf-8 -*-
##################################################
# GNU Radio Python Flow Graph
# Title: Wbfm Wav Nogui
# Generated: Wed Jun  8 23:11:55 2016
##################################################

from gnuradio import analog
from gnuradio import audio
from gnuradio import blocks
from gnuradio import eng_notation
from gnuradio import filter
from gnuradio import gr
from gnuradio.eng_option import eng_option
from gnuradio.filter import firdes
from optparse import OptionParser
import osmosdr
import time

class wbfm_wav_nogui(gr.top_block):

    def __init__(self):
        gr.top_block.__init__(self, "Wbfm Wav Nogui")

        ##################################################
        # Variables
        ##################################################
        self.samp_rate = samp_rate = int(250e3)
        self.freq = freq = int(101.5e6)
        self.am3 = am3 = 0
        self.am2 = am2 = 0
        self.am1 = am1 = 0
        self.Interpolation = Interpolation = 0.875
        self.CH2 = CH2 = '/home/adam/hackRfProj/test.wav'
        self.CH1 = CH1 = '/home/adam/hackRfProj/kalimaba.wav'

        ##################################################
        # Blocks
        ##################################################
        self.osmosdr_sink_0 = osmosdr.sink( args="numchan=" + str(1) + " " + "" )
        self.osmosdr_sink_0.set_sample_rate(samp_rate*4)
        self.osmosdr_sink_0.set_center_freq(freq, 0)
        self.osmosdr_sink_0.set_freq_corr(0, 0)
        self.osmosdr_sink_0.set_gain(14, 0)
        self.osmosdr_sink_0.set_if_gain(40, 0)
        self.osmosdr_sink_0.set_bb_gain(20, 0)
        self.osmosdr_sink_0.set_antenna("", 0)
        self.osmosdr_sink_0.set_bandwidth(250000, 0)
          
        self.fractional_interpolator_xx_0_0_0 = filter.fractional_interpolator_ff(1, Interpolation)
        self.fractional_interpolator_xx_0_0 = filter.fractional_interpolator_ff(1, Interpolation)
        self.fractional_interpolator_xx_0 = filter.fractional_interpolator_ff(1, Interpolation)
        self.blocks_wavfile_source_0_0 = blocks.wavfile_source(CH2, True)
        self.blocks_wavfile_source_0 = blocks.wavfile_source(CH1, True)
        self.blocks_stream_mux_0_0_0 = blocks.stream_mux(gr.sizeof_float*1, (1, 1))
        self.blocks_stream_mux_0_0 = blocks.stream_mux(gr.sizeof_float*1, (1, 1))
        self.blocks_stream_mux_0 = blocks.stream_mux(gr.sizeof_float*1, (1, 1))
        self.blocks_multiply_const_vxx_2 = blocks.multiply_const_vff((am2, ))
        self.blocks_multiply_const_vxx_1 = blocks.multiply_const_vff((am1, ))
        self.blocks_multiply_const_vxx_0 = blocks.multiply_const_vff((am3, ))
        self.blocks_add_xx_0 = blocks.add_vff(1)
        self.audio_source_0 = audio.source(44100, "", True)
        self.analog_wfm_tx_0 = analog.wfm_tx(
        	audio_rate=32000,
        	quad_rate=320000,
        	tau=0.0003,
        	max_dev=75000,
        )

        ##################################################
        # Connections
        ##################################################
        self.connect((self.analog_wfm_tx_0, 0), (self.osmosdr_sink_0, 0))    
        self.connect((self.audio_source_0, 0), (self.blocks_stream_mux_0, 0))    
        self.connect((self.audio_source_0, 1), (self.blocks_stream_mux_0, 1))    
        self.connect((self.blocks_add_xx_0, 0), (self.analog_wfm_tx_0, 0))    
        self.connect((self.blocks_multiply_const_vxx_0, 0), (self.fractional_interpolator_xx_0_0_0, 0))    
        self.connect((self.blocks_multiply_const_vxx_1, 0), (self.fractional_interpolator_xx_0, 0))    
        self.connect((self.blocks_multiply_const_vxx_2, 0), (self.fractional_interpolator_xx_0_0, 0))    
        self.connect((self.blocks_stream_mux_0, 0), (self.blocks_multiply_const_vxx_1, 0))    
        self.connect((self.blocks_stream_mux_0_0, 0), (self.blocks_multiply_const_vxx_2, 0))    
        self.connect((self.blocks_stream_mux_0_0_0, 0), (self.blocks_multiply_const_vxx_0, 0))    
        self.connect((self.blocks_wavfile_source_0, 0), (self.blocks_stream_mux_0_0, 0))    
        self.connect((self.blocks_wavfile_source_0, 1), (self.blocks_stream_mux_0_0, 1))    
        self.connect((self.blocks_wavfile_source_0_0, 0), (self.blocks_stream_mux_0_0_0, 0))    
        self.connect((self.blocks_wavfile_source_0_0, 1), (self.blocks_stream_mux_0_0_0, 1))    
        self.connect((self.fractional_interpolator_xx_0, 0), (self.blocks_add_xx_0, 0))    
        self.connect((self.fractional_interpolator_xx_0_0, 0), (self.blocks_add_xx_0, 1))    
        self.connect((self.fractional_interpolator_xx_0_0_0, 0), (self.blocks_add_xx_0, 2))    

    def get_samp_rate(self):
        return self.samp_rate

    def set_samp_rate(self, samp_rate):
        self.samp_rate = samp_rate
        self.osmosdr_sink_0.set_sample_rate(self.samp_rate*4)

    def get_freq(self):
        return self.freq

    def set_freq(self, freq):
        self.freq = freq
        self.osmosdr_sink_0.set_center_freq(self.freq, 0)

    def get_am3(self):
        return self.am3

    def set_am3(self, am3):
        self.am3 = am3
        self.blocks_multiply_const_vxx_0.set_k((self.am3, ))

    def get_am2(self):
        return self.am2

    def set_am2(self, am2):
        self.am2 = am2
        self.blocks_multiply_const_vxx_2.set_k((self.am2, ))

    def get_am1(self):
        return self.am1

    def set_am1(self, am1):
        self.am1 = am1
        self.blocks_multiply_const_vxx_1.set_k((self.am1, ))

    def get_Interpolation(self):
        return self.Interpolation

    def set_Interpolation(self, Interpolation):
        self.Interpolation = Interpolation
        self.fractional_interpolator_xx_0.set_interp_ratio(self.Interpolation)
        self.fractional_interpolator_xx_0_0.set_interp_ratio(self.Interpolation)
        self.fractional_interpolator_xx_0_0_0.set_interp_ratio(self.Interpolation)

    def get_CH2(self):
        return self.CH2

    def set_CH2(self, CH2):
        self.CH2 = CH2

    def get_CH1(self):
        return self.CH1

    def set_CH1(self, CH1):
        self.CH1 = CH1


def main(top_block_cls=wbfm_wav_nogui, options=None):
    tb = top_block_cls()
    tb.start()
    try:
        raw_input('Press Enter to quit: ')
    except EOFError:
        pass
    tb.stop()
    tb.wait()



if __name__ == '__main__':
    main()
